using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Collections;
using System.Data;
using System.ComponentModel;
using System.Data.OracleClient;
using WPFExcelReport.Helpers;
using WPFExcelReport.Controls;
namespace WPFExcelReport
{
    public partial class VWBZSUM
    {
        public DataRow Convert2WBZSUM(DataRow dwInput, string ITXDBVERSION, bool IsConvert = false)
        {
            DataSet SResult = new DataSet();
            SResult = QueryWBZSUM(dwInput, 0);
            DataRow dwReturn = null;
            if (SResult.Tables[0].Rows.Count > 0)
            {
                dwReturn = SResult.Tables[0].Rows[0];
            }
            else
            {
                dwReturn = SResult.Tables[0].NewRow();
            }
            switch (ITXDBVERSION)
            {
                case "YGR":
                    #region=========
         dwReturn["WBZMANDANT"] = " ".PadRight(2 ); 
         dwReturn["WBZKUNDNR"] = " ".PadRight(8 ); 
         dwReturn["WBZWERK"] = " ".PadRight(3 ); 
         dwReturn["WBZABT"] = " ".PadRight(3 ); 
         dwReturn["WBZWAKZ"] = " ".PadRight(1 ); 
         dwReturn["WBZART"] = " ".PadRight(20 ); 
         dwReturn["WBZDESS"] = " ".PadRight(10 ); 
         dwReturn["WBZCOLORIT"] = " ".PadRight(10 ); 
         dwReturn["WBZFARBE"] = " ".PadRight(10 ); 
         dwReturn["WBZBESCHICH"] = " ".PadRight(10 ); 
         dwReturn["WBZZUSTAND"] = " ".PadRight(4 ); 
         dwReturn["WBZVAR"] = " ".PadRight(2 ); 
         dwReturn["WBZZENSUR"] = " ".PadRight(1 ); 
         dwReturn["WBZZENSURKZ"] = " ".PadRight(1 ); 
         dwReturn["WBZLO"] = " ".PadRight(2 ); 
         dwReturn["WBZLB"] = " ".PadRight(2 ); 
         dwReturn["WBZAUFMSL"] = " ".PadRight(2 ); 
         dwReturn["WBZDPART"] = " ".PadRight(11 ); 
         dwReturn["WBZLPGEBNR"] = " ".PadRight(8 ); 
         dwReturn["WBZAUFTYP"] = " ".PadRight(1 ); 
         dwReturn["WBZAUFKRS"] = " ".PadRight(2 ); 
         dwReturn["WBZAUFLFD"] = " ".PadRight(6 ); 
         dwReturn["WBZAUFPOS"] = " ".PadRight(3 ); 
         dwReturn["WBZTP"] = " ".PadRight(4 ); 
         dwReturn["WBZUPOS"] = " ".PadRight(2 ); 
         dwReturn["WBZLAEKAT"] = " ".PadRight(6 ); 
         dwReturn["WBZBRTKAT"] = " ".PadRight(4 ); 
         dwReturn["WBZDART"] = " ".PadRight(20 ); 
         dwReturn["WBZDVAR"] = " ".PadRight(2 ); 
         dwReturn["WBZMTR"] = " ".PadRight(0 ); 
         dwReturn["WBZQM"] = " ".PadRight(0 ); 
         dwReturn["WBZSTK"] = " ".PadRight(0 ); 
         dwReturn["WBZDKZ"] = " ".PadRight(1 ); 
         dwReturn["WBZFILLER"] = " ".PadRight(47 ); 
                    #endregion
                    break;
                case "JLT":

                    break;
                default:

                    break;
            }
            if (IsConvert)
            {
                foreach (DataColumn cm in dwInput.Table.Columns)
                {
                    if (dwInput[cm.ColumnName].ToString() == "")
                        continue;
                    if (dwReturn.Table.Columns.Contains(cm.ColumnName))
                    {
                        dwReturn[cm.ColumnName] = dwInput[cm.ColumnName];
                    }
                }
            }
            return dwReturn;
        }
        public void MapWBZSUM(ref DataRow dwLocal, DataRow dwData)
        {
            BlindMap(ref dwLocal, dwData);
        }
        public void SaveWBZSUM(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            DataSet setResult = new DataSet();
            DataTable tbLocal = null;
            DataRow dwLocal = null;
            bool flgExist = false;
            setResult = QueryWBZSUM(dwData);
            if (setResult.Tables.Count > 0)
            {
                tbLocal = setResult.Tables[0];
                if (tbLocal.Rows.Count > 0)
                {
                    flgExist = true;
                    dwLocal = tbLocal.Rows[0];
                }
                else
                {
                    dwLocal = tbLocal.NewRow();
                }
            }
            MapWBZSUM(ref dwLocal, dwData);

            if (!flgExist)
            {

                InsertWBZSUM(dwLocal, ITXDBVERSION);
            }
            UpdateWBZSUM(dwLocal);
        }
        public void InsertWBZSUM(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            OracleCommand or1;
            or1 = OracleDB.CreateCommand();
            switch (ITXDBVERSION)
            {
                case "YGR":
                    or1.CommandText = "INSERT INTO WBZSUM(VAKBMANDANT, VAKBKOLL, VAKBART, VAKBKUNDNR, VAKBFARBE, VAKBKDARTNR, VAKBKDARTBEZ, VAKBKDFARBNR, VAKBANDRUCK, VAKBMEGEW, VAKBMENGEH, VAKBGEWVON, VAKBGEWBIS, VAKBGEWCA, VAKBGEWFKT, VAKBGEWGRP, VAKBMEGFKT, VAKBMEGGRP, VAKBAENDZL, VAKBAENDBEN, VAKBAENDDAT, VAKBNEUBEN, VAKBNEUDAT, VAKBFILLER)"
                        + " VALUES (:VAKBMANDANT, :VAKBKOLL, :VAKBART, :VAKBKUNDNR, :VAKBFARBE, '                         ', '                         ', '                             ', ' ', '   ', '   ', 0, 0, ' ', '000000000000', ' ', '000000000000', ' ', 0, '        ', '000000', '        ', '      ', '                                          ')";
                    or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                //VAFMANDANT VAFKOLL VAFART VAFFARBE VAFBEZ VAFFFARBE

            }
            
            Connect2Proxy(or1);
        }
        public DataSet QueryWBZSUM(DataRow dwData, int option = 1)
        {
            DataSet Return;
            DataSet Result = new DataSet();
            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 0:
                    or1.CommandText = "SELECT * FROM WBZSUM WHERE  ROWNUM<2 ";
                    break;
              
                case 99:
                    or1.CommandText = "SELECT * FROM WBZSUM  WHERE ROWNUM<50";
                    break;
                default:
                    break;
            }
            Result = Connect2Proxy(or1);
            if (Result.Tables.Count > 0)
                Result.Tables[0].TableName = "WBZSUM";
            Return = Result;
            return Return;
        }
        public void DeleteWBZSUM(DataRow dwData, int option = 1)
        {

            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    or1.CommandText = "DELETE FROM WBZSUM   WHERE  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                     or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                default:
                    break;
            }
            Connect2Proxy(or1);
        } 
      
        public void UpdateWBZSUM(DataRow dwData, string ITXDBVERSION = "50.24", int option = 1)
        {
            OracleCommand or1;
            DataSet Result = new DataSet();
            or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    #region====Command==== 
                    or1.CommandText = "UPDATE WBZSUM SET "
                    +"WBZMANDANT= :WBZMANDANT,"
                    +"WBZKUNDNR= :WBZKUNDNR,"
                    +"WBZWERK= :WBZWERK,"
                    +"WBZABT= :WBZABT,"
                    +"WBZWAKZ= :WBZWAKZ,"
                    +"WBZART= :WBZART,"
                    +"WBZDESS= :WBZDESS,"
                    +"WBZCOLORIT= :WBZCOLORIT,"
                    +"WBZFARBE= :WBZFARBE,"
                    +"WBZBESCHICH= :WBZBESCHICH,"
                    +"WBZZUSTAND= :WBZZUSTAND,"
                    +"WBZVAR= :WBZVAR,"
                    +"WBZZENSUR= :WBZZENSUR,"
                    +"WBZZENSURKZ= :WBZZENSURKZ,"
                    +"WBZLO= :WBZLO,"
                    +"WBZLB= :WBZLB,"
                    +"WBZAUFMSL= :WBZAUFMSL,"
                    +"WBZDPART= :WBZDPART,"
                    +"WBZLPGEBNR= :WBZLPGEBNR,"
                    +"WBZAUFTYP= :WBZAUFTYP,"
                    +"WBZAUFKRS= :WBZAUFKRS,"
                    +"WBZAUFLFD= :WBZAUFLFD,"
                    +"WBZAUFPOS= :WBZAUFPOS,"
                    +"WBZTP= :WBZTP,"
                    +"WBZUPOS= :WBZUPOS,"
                    +"WBZLAEKAT= :WBZLAEKAT,"
                    +"WBZBRTKAT= :WBZBRTKAT,"
                    +"WBZDART= :WBZDART,"
                    +"WBZDVAR= :WBZDVAR,"
                    +"WBZMTR= :WBZMTR,"
                    +"WBZQM= :WBZQM,"
                    +"WBZSTK= :WBZSTK,"
                    +"WBZDKZ= :WBZDKZ,"
                    +"WBZFILLER= :WBZFILLER,"
                        + " WHERE  "
                        + "  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                    #endregion

                    #region====Parameters====
          or1.Parameters.Add("WBZMANDANT", OracleType.NVarChar).Value = dwData["WBZMANDANT"].ToString(); 
          or1.Parameters.Add("WBZKUNDNR", OracleType.NVarChar).Value = dwData["WBZKUNDNR"].ToString(); 
          or1.Parameters.Add("WBZWERK", OracleType.NVarChar).Value = dwData["WBZWERK"].ToString(); 
          or1.Parameters.Add("WBZABT", OracleType.NVarChar).Value = dwData["WBZABT"].ToString(); 
          or1.Parameters.Add("WBZWAKZ", OracleType.NVarChar).Value = dwData["WBZWAKZ"].ToString(); 
          or1.Parameters.Add("WBZART", OracleType.NVarChar).Value = dwData["WBZART"].ToString(); 
          or1.Parameters.Add("WBZDESS", OracleType.NVarChar).Value = dwData["WBZDESS"].ToString(); 
          or1.Parameters.Add("WBZCOLORIT", OracleType.NVarChar).Value = dwData["WBZCOLORIT"].ToString(); 
          or1.Parameters.Add("WBZFARBE", OracleType.NVarChar).Value = dwData["WBZFARBE"].ToString(); 
          or1.Parameters.Add("WBZBESCHICH", OracleType.NVarChar).Value = dwData["WBZBESCHICH"].ToString(); 
          or1.Parameters.Add("WBZZUSTAND", OracleType.NVarChar).Value = dwData["WBZZUSTAND"].ToString(); 
          or1.Parameters.Add("WBZVAR", OracleType.NVarChar).Value = dwData["WBZVAR"].ToString(); 
          or1.Parameters.Add("WBZZENSUR", OracleType.NVarChar).Value = dwData["WBZZENSUR"].ToString(); 
          or1.Parameters.Add("WBZZENSURKZ", OracleType.NVarChar).Value = dwData["WBZZENSURKZ"].ToString(); 
          or1.Parameters.Add("WBZLO", OracleType.NVarChar).Value = dwData["WBZLO"].ToString(); 
          or1.Parameters.Add("WBZLB", OracleType.NVarChar).Value = dwData["WBZLB"].ToString(); 
          or1.Parameters.Add("WBZAUFMSL", OracleType.NVarChar).Value = dwData["WBZAUFMSL"].ToString(); 
          or1.Parameters.Add("WBZDPART", OracleType.NVarChar).Value = dwData["WBZDPART"].ToString(); 
          or1.Parameters.Add("WBZLPGEBNR", OracleType.NVarChar).Value = dwData["WBZLPGEBNR"].ToString(); 
          or1.Parameters.Add("WBZAUFTYP", OracleType.NVarChar).Value = dwData["WBZAUFTYP"].ToString(); 
          or1.Parameters.Add("WBZAUFKRS", OracleType.NVarChar).Value = dwData["WBZAUFKRS"].ToString(); 
          or1.Parameters.Add("WBZAUFLFD", OracleType.NVarChar).Value = dwData["WBZAUFLFD"].ToString(); 
          or1.Parameters.Add("WBZAUFPOS", OracleType.NVarChar).Value = dwData["WBZAUFPOS"].ToString(); 
          or1.Parameters.Add("WBZTP", OracleType.NVarChar).Value = dwData["WBZTP"].ToString(); 
          or1.Parameters.Add("WBZUPOS", OracleType.NVarChar).Value = dwData["WBZUPOS"].ToString(); 
          or1.Parameters.Add("WBZLAEKAT", OracleType.NVarChar).Value = dwData["WBZLAEKAT"].ToString(); 
          or1.Parameters.Add("WBZBRTKAT", OracleType.NVarChar).Value = dwData["WBZBRTKAT"].ToString(); 
          or1.Parameters.Add("WBZDART", OracleType.NVarChar).Value = dwData["WBZDART"].ToString(); 
          or1.Parameters.Add("WBZDVAR", OracleType.NVarChar).Value = dwData["WBZDVAR"].ToString(); 
          or1.Parameters.Add("WBZMTR", OracleType.NVarChar).Value = dwData["WBZMTR"].ToString(); 
          or1.Parameters.Add("WBZQM", OracleType.NVarChar).Value = dwData["WBZQM"].ToString(); 
          or1.Parameters.Add("WBZSTK", OracleType.NVarChar).Value = dwData["WBZSTK"].ToString(); 
          or1.Parameters.Add("WBZDKZ", OracleType.NVarChar).Value = dwData["WBZDKZ"].ToString(); 
          or1.Parameters.Add("WBZFILLER", OracleType.NVarChar).Value = dwData["WBZFILLER"].ToString(); 
                    #endregion
                    break; 
                default:
                    break;
            }


            Connect2Proxy(or1);
        }
    }
}
